home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-26 | 10.5 KB | 414 lines | [TEXT/KAHL] |
-
-
- // I'm rethinking the sound thing ...
- // what if I don't have a set of various sounds ?
- // instead, take one sound - different pitches ... and let the program
- // play the various pitches randomly (== music? I think not)
- // later I can refine the randomness to play ping noise
- // so there is *some* rhyme and reason to the random notes :)
-
- #include "GraphicsModule_Types.h"
- #include "Sounds.h"
-
-
- // these are the functs that need defined ...
- OSErr DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params);
- OSErr DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
- OSErr DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
- OSErr DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
- OSErr DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params);
- // these must be defined also
- OSErr DoSelected(RgnHandle blankRgn, short message, GMParamBlockPtr params);
- OSErr DoAboutBox(RgnHandle blankRgn, short message, GMParamBlockPtr params);
-
- short gTheMonitor;
-
- #define FlipACoin() (RangedRdm(0, 100) > 50)
-
- SoundInfoHandle gSndInfoHandle;
- #define BASE_NOTE_ID 128
- #define BASE_BEAT_ID 1000
-
- #define MAX_NOTES 9
- #define MAX_BEATS 2
-
- Handle gNotes[MAX_NOTES]; // how many? E f G a B c D e F == 9 ? plus silence!
- Handle gBeats[MAX_BEATS];
-
- Boolean gDoSound = FALSE;
-
- short gCurrentNote = 0;
- long gNotesPlayed = 0;
-
- long gNoteStop = 0;
-
- unsigned short RangedRdm( unsigned short min, unsigned short max );
-
- short gState, gDimness;
- enum {
- DIMMING,
- DONE,
- STOP
- };
-
- //////////////////////////////////////////////////////////////////////////////////////
- // this is the first funct called by AD ... we need to allocate and initialize here
- OSErr
- DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params) {
- void initsphere(int screen, Rect *theRect);
-
- gDimness = 255;
-
- for (gTheMonitor=0;
- (gTheMonitor < params->monitors->monitorCount && gTheMonitor < 4);
- gTheMonitor++)
- initsphere(gTheMonitor,
- &(params->monitors->monitorList[gTheMonitor].bounds) );
-
- GetDateTime((unsigned long *) ¶ms->qdGlobalsCopy->qdRandSeed);
-
- #ifdef DO_SOUND
- if (params->systemConfig & SoundAvailable) {
- short i;
-
- gDoSound = TRUE;
- for (i = 0; i<MAX_NOTES; i++)
- gNotes[i] = GetResource('snd ', BASE_NOTE_ID + i);
- for (i=0; i<MAX_BEATS; i++)
- gBeats[i] = GetResource('snd ', BASE_BEAT_ID + i);
-
- /* to use the sound functions in AD 2.0u we must pass in "params" */
- gSndInfoHandle = OpenSound(params);
- } else gDoSound = FALSE;
- #else
- gDoSound = FALSE;
- #endif
-
- gTheMonitor = 0;
- return noErr;
- }
-
- //////////////////////////////////////////////////////////////////////////////////////
- // the screen saver has been awakened! time to ditch the storage and wave goodbye
- OSErr
- DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) {
- short i;
-
- params->brightness = 255;
-
-
- #ifdef DO_SOUND
- if (gDoSound ) {
- CloseSound(gSndInfoHandle, params->sndChannel);
-
- for (i=0; i<MAX_NOTES; i++) {
- if (gNotes[i] != NULL )
- ReleaseResource( gNotes[i] );
- }
-
- for (i=0; i<MAX_BEATS; i++){
- if (gBeats[i] != NULL)
- ReleaseResource( gBeats[i] );
- }
-
- }
- #endif
- return noErr;
- }
-
-
-
- //////////////////////////////////////////////////////////////////////////////////////
- // make the screen go black
- OSErr
- DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) {
-
- if (params->controlValues[0])
- gState = DIMMING;
- else
- gState = STOP;
- return noErr;
-
- }
-
- //////////////////////////////////////////////////////////////////////////////////////
- // this is the workhorse routine. It does the continual screen work to make
- // this screen saver what it is.
- OSErr
- DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
- {
- void drawsphere(int screen, int screenDepth);
- int Dim(void);
-
- //// dimming code ...
- if ( gState == DIMMING) {
- params->brightness = Dim();
- if (params->brightness == 0)
- gState = DONE;
- return noErr;
- }
- if ( gState == DONE) {
- FillRgn(blankRgn, params->qdGlobalsCopy->qdBlack);
- params->brightness = 255;
- gState = STOP;
- }
- ////
-
-
- if (gTheMonitor >= 4 || gTheMonitor >= params->monitors->monitorCount)
- gTheMonitor = 0;
-
- #ifdef DO_SOUND
- // There is a sound ? and we are available to deal with sounds?
- if (gDoSound &&
- (
- // (!SoundBusy(gSndInfoHandle, params->sndChannel)) ||
- (TickCount() >= gNoteStop))
- ) {
-
- gNotesPlayed++; // up our count
-
- if ( !(gNotesPlayed % 4)) { // time for a beat ?
- gCurrentNote = RangedRdm(0, MAX_BEATS-1);
- gNoteStop = TickCount() + 90;
- if (gBeats[ gCurrentNote ] != NULL)
- PlaySound( gSndInfoHandle, params->sndChannel,
- gBeats[ gCurrentNote ] );
- } else { // time for a note now
-
- if (FlipACoin()) // 50/50 chance
- gCurrentNote = gCurrentNote - RangedRdm(1, 2);
- else
- gCurrentNote = gCurrentNote + RangedRdm(1, 2);
-
- if (gCurrentNote >= MAX_NOTES) // come down from the top the overflow amt.
- gCurrentNote = (MAX_NOTES-1) - (gCurrentNote - MAX_NOTES);
-
- if (gCurrentNote < 0) // flip it to be > 0
- gCurrentNote = -gCurrentNote;
-
- // how long of a note?
- if (!(gNotesPlayed % 5))
- gNoteStop = TickCount() + 60;
- else {
- short minticks, maxticks;
-
- minticks = params->controlValues[1] / 5;
- maxticks = 30 + (params->controlValues[2] / 5);
- gNoteStop = TickCount() +
- RangedRdm( minticks, maxticks);
- }
- if ( gNotes[ gCurrentNote ] != NULL )
- PlaySound( gSndInfoHandle, params->sndChannel, gNotes[ gCurrentNote] );
- }
- }
- #endif
-
-
- drawsphere( gTheMonitor,
- params->monitors->monitorList[gTheMonitor].curDepth);
-
- gTheMonitor++;
- return noErr;
- }
-
- //////////////////////////////////////////////////////////////////////////////////////
- // this is called when they click on something in the control panel
- OSErr
- DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params) {
- // button got pushed??
- return noErr;
- }
-
-
-
- OSErr DoSelected(RgnHandle blankRgn, short message, GMParamBlockPtr params) {
- return noErr;
- }
-
-
-
- OSErr DoAboutBox(RgnHandle blankRgn, short message, GMParamBlockPtr params) {
- void initsphere(int screen, Rect *theRect);
- Boolean HasColorQD(void);
- GrafPtr savePort;
- PicHandle pHandle;
- Boolean GetOut = FALSE;
- unsigned char thePatterns[6][8] = {
- 0x0020, 0x0000, 0x0002, 0x0000,
- 0x8010, 0x0220, 0x0108, 0x4004,
- 0x8822, 0x8822, 0x8822, 0x8822,
- 0xAA55, 0xAA55, 0xAA55, 0xAA55,
- 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
- 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
- };
-
-
- gDoSound = FALSE;
-
- gTheMonitor = 0;
- GetPort( &savePort);
- EraseRect( &savePort->portRect);
-
-
- initsphere(0, &(savePort->portRect) );
- // can we somehow make a "mask" of text ... so the
- // spheres will not be able to draw over it ...
- // then - the text will emerge from the image as the
- // spheres draw about
- ForeColor(blackColor);
- BackColor(whiteColor);
-
- // now ... show it all, animate it ... wait for user click
- while (!Button()) {
- if (HasColorQD())
- drawsphere( 0, (*((CGrafPtr)savePort)->portPixMap)->pixelSize );
- else
- drawsphere(0, 1);
- }
-
- // user clicked .. now fade in our message ...
- pHandle = GetPicture( 128 ); // about message in B&W
- if (pHandle) {
- BitMap bm, bm2;
- int width;
- GrafPort gp;
- Boolean DITCHFLAG = FALSE;
-
- ////// create an offscreen bitmap (size of the screen window) for the picture
- width = savePort->portRect.right - savePort->portRect.left;
- bm.rowBytes = ((width + 15) / 16) * 2;
- bm.bounds.left = 0;
- bm.bounds.right = width;
- bm.bounds.bottom = savePort->portRect.bottom - savePort->portRect.top;
- bm.bounds.top = 0;
-
- //DebugStr("\pNewPtrClear bm.baseAddr");
-
- bm.baseAddr = NewPtrClear((long)bm.rowBytes * bm.bounds.bottom);
- if (bm.baseAddr != NULL) {
- short wDiff, hDiff;
- OpenPort( &gp);
- SetPortBits( &bm);
- BackPat( &thePatterns[5][0]);
-
- BlockMove( (Ptr)&bm.bounds, (Ptr)&gp.portRect, sizeof(Rect));
- RectRgn( gp.visRgn, &bm.bounds);
-
- // ok, now draw the picture in that offscreen port ...
-
- wDiff = (bm.bounds.right - bm.bounds.left) -
- ( (**pHandle).picFrame.right -(**pHandle).picFrame.left);
- hDiff = (bm.bounds.bottom- bm.bounds.top) -
- ( (**pHandle).picFrame.bottom -(**pHandle).picFrame.top);
-
- // if the offscreen bitmap is larger than the pict bounds, center it
- if ( wDiff > 0 || hDiff > 0 ) {
- Rect theRect;
-
- // FILL ME black
- BackColor( blackColor); // black!
- EraseRect( &bm.bounds); // clear out those white edges that will come
- BackColor( whiteColor); // set back to white
-
- if (wDiff < 0) wDiff = 0; // lets not go negative!!!!
- if (hDiff < 0) hDiff = 0; // (the pict will slide offscreen!
- theRect = (**pHandle).picFrame;
- OffsetRect( &theRect, wDiff/2, hDiff/2);
- DrawPicture( pHandle, &theRect );
-
- } else
- DrawPicture( pHandle, &bm.bounds); // just blit/scrunch it in
-
- // OK, bm has the text message picture in it now! whew!
-
- /////////////// dup a bitmap to that one
- BlockMove( &bm, &bm2, sizeof(BitMap) ); // copy the bitmap
-
- //DebugStr("\pNewPtrClear bm2.baseAddr");
-
- // but we do need to allocate our own picture area ...
- bm2.baseAddr = NewPtrClear((long)bm.rowBytes * bm.bounds.bottom);
- if (bm2.baseAddr != NULL) {
- int i;
-
- SetPort(&gp);
- SetPortBits( &bm2);
- BlockMove( (Ptr)&bm2.bounds, (Ptr)&gp.portRect, sizeof(Rect));
- RectRgn( gp.visRgn, &bm2.bounds);
-
- //SetPort( savePort);
- //ForeColor(blackColor);
- //PenMode( patXor);
-
- for (i = 0; i < 6 && !GetOut; i++) {
- short x, y;
-
- SetPort(&gp);
- SetPortBits( &bm2);
-
- // fill the "mask" bitmap, bm2, with a pattern
- FillRect( &(gp.portRect), &thePatterns[i][0]);
-
- // copy the image into the main window using the mask
- CopyMask(
- &bm,
- &bm2,
- &(savePort->portBits),
- &bm.bounds,
- &bm2.bounds,
- &savePort->portRect);
-
- }
- } else DITCHFLAG = TRUE;
- } else DITCHFLAG = TRUE;
- SetPort( savePort);
-
- //DebugStr("\pClosing port and disposing ptrs");
-
- // ditch the port
- ClosePort( &gp);
- // ditch the bitmaps
- DisposPtr(bm.baseAddr);
- DisposPtr(bm2.baseAddr);
-
- // didn't happen ... then just draw the picture
- if (DITCHFLAG == TRUE)
- DrawPicture( pHandle, &(savePort->portRect) );
-
- ReleaseResource( (Handle)pHandle);
- } else SysBeep(0);
-
- while (!Button() && !GetOut)
- ;
- // ditch events for Darkside to be happy
- FlushEvents( everyEvent, 0);
-
- return noErr;
- }
-
-
- Boolean
- HasColorQD(void) {
- OSErr err;
- Boolean answer = true;
- long gestaltResult;
-
- err = Gestalt(gestaltQuickdrawVersion, &gestaltResult);
-
- answer = (err == noErr) && (gestaltResult >= gestalt8BitQD);
- return answer;
- }
-
-
- int
- Dim(void) {
- gDimness -= 10;
- if (gDimness <= 0) {
- gDimness = 0;
- }
- return gDimness;
- }
-
-
-